// 数据请求服务等待窗口
import { Loading } from 'element-ui';

let loadingCount: number = 0;
let loading: any;
let timer: any;

const startLoading = () => {
  loading = Loading.service({
    lock: true,
    text: '拼命加载中...',
    background: 'rgba(0,0,0,0.7)',
  });
};

const endLoading = () => {
  if (timer) {
    clearTimeout(timer);
  }
  loading.close();
};

export const showLoading = () => {
  if (loadingCount === 0) {
    startLoading();
  }
  loadingCount += 1;
  if (timer) {
    clearTimeout(timer);
  }
  timer = setTimeout(() => {
    endLoading();
  }, 10000);
};

export const hideLoading = () => {
  if (loadingCount <= 0) {
    return;
  }
  loadingCount -= 1;
  if (loadingCount === 0) {
    endLoading();
  }
};
